<?

class Defensio {

    var $d_url = 'http://api.defensio.com/app/1.2/';

    var $api_key;

    var $owner_url;

    function Defensio() {

        $CI =& get_instance();

        $this->api_key = $CI->config->item('defensio_api_key');

        $this->owner_url = base_url();

    }

    function audit_comment($user_ip, $article_date, $comment_author, $comment_content) {

        if (!$comment_author) {

            $comment_author = 'Jesse';

        }

        $url = $this->d_url . 'audit-comment/' . $this->api_key . '.xml';

        $postfields = array(
            'owner-url'			=>	$this->owner_url,
            'user-ip'			=>	$user_ip,
            'article-date'		=>	$article_date,
            'comment-author'	=>	$comment_author,
            'comment-type'		=>	'comment',
            'comment-content'	=>	$comment_content);

        $results = $this->execute_post($url, $postfields);

        if ($results->spam == 'false') {

        // no spam here!
            return FALSE;

        }

        else {

        // kill the spammers!
            return TRUE;
            
        }

    }

    function execute_post($url, $postfields) {

        $ch = curl_init();

        // set the target url
        curl_setopt($ch, CURLOPT_URL,$url);

        // howmany parameter to post
        curl_setopt($ch, CURLOPT_POST, 1);

        // the parameter 'username' with its value 'johndoe'
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

        $result = curl_exec ($ch);
        curl_close ($ch);

        return simplexml_load_string($result);

    }

}

?>